package net.tngou.crawler.drug;

import java.io.ObjectInputStream.GetField;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.lang3.math.NumberUtils;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONObject;

import net.tngou.c4j.util.HttpUtil;
import net.tngou.cache.CacheEngine;
import net.tngou.cache.TgCacheEngine;
import net.tngou.crawler.util.StringUtil;

/**
 * 康泽大药房
 * @author tngou@tngou.net
 *
 */
public class COM_173kz extends BaseCrawler implements CrawlerDrug{
	
	private static final Logger log= LoggerFactory.getLogger(COM_173kz.class);

	private String url="http://www.173kz.com/goods/product-${}.html";
	


	@Override
	public JSONObject getJson(URL url) {
		log.info("请求页面:{}",url.toString());
		Document doc = HttpUtil.get(url.toString());
		if(doc==null)return null;
		json.put("url", url.toString());
		json.put("onlinepharmacy", onlinepharmacy);
		//唯一值
		json.put("un",url.toString().split("-")[1].replaceAll(".html", "-").split("-")[0]);
		//标签
	
		Elements crumbs = doc.getElementsByClass("crumbs");
		if(crumbs.first()!=null)
		{
				Elements as = crumbs.first().getElementsByTag("a");
				List<String> list = new ArrayList<>();
				for (int i = 1; i < as.size()-1; i++) {
					list.add(as.get(i).text().replaceAll(" >", ""));
				}				
				json.put("tag", list);
		}
		//价格
		Elements kz = doc.getElementsByClass("kz");
		if(kz.first()!=null)
		{
			String p = kz.first().getElementsByTag("p").text();	
			json.put("price", getPrice(p));
			
		}
		//图片		
		Elements cp_xq = doc.getElementsByClass("cp_xq");
		if(cp_xq.first()!=null)
		{
			Elements imgs = cp_xq.first().getElementsByTag("img");
			List<String> list = new ArrayList<>();
			imgs.forEach(e->{
				String src = e.attr("src");
				if(StringUtil.isNotEmpty(src))
				{
					list.add(src);
				}
			});
			json.put("picture", list);
		}
		//类型
		if(doc.text().toLowerCase().indexOf("RX")>=0)
		{
			json.put("ptype", Ptype.RX处方药.ordinal());
		}else if(doc.text().toLowerCase().indexOf("REDOTC")>=0)
		{
			json.put("ptype", Ptype.OTC非处方甲类.ordinal());			
		}else
		{json.put("ptype", Ptype.OTC非处方乙类.ordinal());}
		//说明书
		Elements sp_c_box = doc.getElementsByClass("sp_c_box");
		if(sp_c_box.first()!=null)
		{
			JSONObject instructions = instructions(sp_c_box.first());	
			json.put("instructions",instructions );
			
			
		}	
		return json;
	}
	
	private JSONObject instructions(Element element) {
		JSONObject json = new JSONObject(true);
		String h2 = element.getElementsByTag("h2").first().text();
		String[] sts = StringUtil.split(h2, " ");
		if(sts.length==1)
			json.put("商品名称", sts[0]);
		else if(sts.length==2)
			json.put("商品名称", sts[1]);
		else if(sts.length>2)
			json.put("商品名称", sts[1]);
		Elements ps = element.getElementsByTag("p");
		ps.forEach(e->{
			String[] es = StringUtil.split(e.text(),"：");
			if(es.length==2)
			{
				if("批准文号".equals(es[0]))
					json.put(es[0],es[1].replaceAll(" ", "").split("\\(")[0]);
				if("生产企业".equals(es[0]))
					json.put(es[0],es[1]);
			}
			
		});
		
		return name_number(json);
	}
	
	
}
